



app.factory("identityService", ["$http", "$q", "$window", "queryparser", "geolocation", "$location", "$exception", "$token", "$canceler", "$indexeddb", function ($http, $q, $window, queryparser, geolocation, $location, $exception, $token, $canceler, $indexeddb) {
    function IdentityService() {
    };
    IdentityService.prototype = {

        
        //getAlertById: function (alertId) {
        //    return $token.get().then(function (token) {
        //        return $http.get("/api/alert/" + alertId, { params: { token: token} }).then(function (res) {
        //            return res.data;
        //        });
        //    });
        //},
        getArchivedAlertById: function (alertId) {
            return $token.get().then(function (token) {
                return $http.get("/api/alert/archive/" + alertId, { params: { token: token} }).then(function (res) {
                    return res.data;
                });
            });
        },
        getNearestAlertsForShopFromToken: function (skip) {
            return $token.get().then(function (token) {
                return $http.get("/api/alert/near/shop", { params: { token: token, skip: skip, limit: 1} }).then(function (res) {
                    return res.data;
                });
            });
        },
        //getSubscribedAlertsForShopFromToken: function (skip) {
        //    return $token.get().then(function (token) {
        //        return $http.get("/api/alert/subscriber/shop", { params: { token: token, skip: skip, limit: 1} }).then(function (res) {
        //            return res.data;
        //        });
        //    });
        //},
        //removeUserAlert: function (alertId) {
        //    return $token.get().then(function (token) {
        //        return $http.post("api/alert/user/delete", { alertId: alertId.toString() }, { params: { token: token} });
        //    });
        //},
        //removeAlert: function (alertId) {
        //    return $token.get().then(function (token) {
        //        return $http.post("api/alert/delete", { alertId: alertId }, { params: { token: token} });
        //    });
        //},
        //subscribeUserToShop: function (shopId) {
        //    return $token.get().then(function (token) {
        //        return $http.post("/api/shop/subscribe/user", { shopId: shopId }, { params: { token: token} });
        //    });
        //},
        //subscribeShopToShop: function (shopId) {
        //    return $token.get().then(function (token) {
        //        return $http.post("/api/shop/subscribe/shop", { shopId: shopId }, { params: { token: token} });
        //    });
        //},
        //unsubscribeUserFromShop: function (shopId) {
        //    return $token.get().then(function (token) {
        //        return $http.post("/api/shop/unsubscribe/user", { shopId: shopId.toString() }, { params: { token: token} });
        //    });
        //},
        unsubscribeShopFromShop: function (shopId) {
            return $token.get().then(function (token) {
                return $http.post("/api/shop/unsubscribe/shop", { shopId: shopId.toString() }, { params: { token: token} });
            });
        },
        getUserSubscribers: function (skip) {
            return $token.get().then(function (token) {
                return $http.get("/api/shop/subscriber/users", { params: { token: token, skip: skip, limit: 1} }).then(function (res) {
                    return res.data;
                });
            });
        },
        getShopsSubscribers: function (skip) {
            return $token.get().then(function (token) {
                return $http.get("/api/shop/subscriber/shops", { params: { token: token, skip: skip, limit: 1} }).then(function (res) {
                    return res.data;
                });
            });
        },
        saveAlert: function (alert) {
            return $token.get().then(function (token) {
                return $http.post("/api/alert/save", alert, { params: { token: token} });
            });
        },
        saveShop: function (data) {
            return $http.post("/api/shop/save", data, { timeout: $canceler.create().promise });
        },
        getShopById: function (shopId) {
            return $http.get("api/shop/" + shopId).then(function (res) {
                return res.data;
            });
        },
        //getFullShopById: function (shopId) {
        //    return $http.get("api/shop/full/" + shopId).then(function (res) {
        //        return res.data;
        //    });
        //},
        //getFullShopFromToken: function () {
        //    return $token.get().then(function (token) {
        //        return $http.get("api/shop/full", { params: { token: token} }).then(function (res) {
        //            return res.data;
        //        });
        //    });
        //},
        getEmptyShop: function () {
            return $http.get("/api/shopcreation/empty").then(function (res) {
                return res.data;
            });
        },
        //getNearestShopForShopId: function (shopId) {
        //    return $token.get().then(function (token) {
        //        return $http.get("/api/shop/near/" + shopId, { params: { token: token, skip: 0, limit: 20} }).then(function (res) {
        //            return res.data;
        //        });
        //    });
        //},
        removeShop: function (shopId) {
            return $token.get().then(function (token) {
                return $http.post("/api/shop/delete", { shopId: shopId }, { params: { token: token} });
            });
        },
        //getShopAlerts: function (shopId, skip) {
        //    return $token.get().then(function (token) {
        //        return $http.get("/api/alert/shop/" + shopId, { params: { token: token, skip: skip} }).then(function (res) {
        //            return res.data;
        //        });
        //    });
        //},
        //getShopAlertsFromToken: function (skip) {
        //    return $token.get().then(function (token) {
        //        return $http.get("/api/alert/shop", { params: { token: token, skip: skip} }).then(function (res) {
        //            return res.data;
        //        });
        //    });
        //},
        //getGpsAlerts: function (pagger, force, timeout) {
        //    var self = this;
        //    force = force || false;
        //    timeout = timeout || 5000;
        //    var uselocalposition = $window.sessionStorage.position && !force;

        //    var params = {
        //        maxDistance: 20000,
        //        minDistance: 0,
        //        num: 1
        //    }
        //    if (pagger) {
        //        if (pagger.minDistance) params.minDistance = pagger.minDistance;
        //        if (pagger.nin) params.nin = pagger.nin;
        //    }

        //    if (uselocalposition) {
        //        var position = JSON.parse($window.sessionStorage.position);
        //        params.lng = position.coords.longitude;
        //        params.lat = position.coords.latitude;
        //        return $http.get("/api/alert/gps", { params: params }).then(function (res) {
        //            return res.data;
        //        });
        //    }
        //    else {
        //        var options = {
        //            enableHighAccuracy: false,
        //            timeout: timeout,
        //            maximumAge: 0
        //        };
        //        return geolocation.getLocation(options).then(function (position) {
        //            $window.sessionStorage.position = JSON.stringify(position);
        //            params.lng = position.coords.longitude;
        //            params.lat = position.coords.latitude;

        //            return $http.get("/api/alert/gps", { params: params }).then(function (res) {
        //                return res.data;
        //            });
        //        }, function (error) {
        //            if ($window.sessionStorage.position)
        //                return self.getGpsAlerts(pagger, false);
        //            else
        //                return self.getGpsAlerts(pagger, true, Infinity);
        //        });
        //    }
        //},
        //getUserPositionsFromToken: function () {
        //    var positions = [];

        //    return this.getFullUserFromToken().then(function (user) {
        //        positions.push({
        //            type: "home",
        //            lng: user.location.loc.coordinates[0],
        //            lat: user.location.loc.coordinates[1]
        //        });

        //        if ($window.sessionStorage.position) {
        //            var position = JSON.parse($window.sessionStorage.position);
        //            positions.push({
        //                type: "gps",
        //                lng: position.coords.longitude,
        //                lat: position.coords.latitude
        //            });
        //        }
        //        return positions;
        //    })
        //},
        getUserById: function (userId) {
            return $http.get("api/user/" + userId).then(function (res) {
                return res.data;
            });
        },
        getUserFromToken: function () {
            return $token.get().then(function (token) {
                return $http.get("api/user", { params: { token: token} }).then(function (res) {
                    return res.data;
                });
            });
        },
        getFullUserById: function (userId) {
            return $http.get("api/user/full/" + userId).then(function (res) {
                return res.data;
            });
        },
        //getFullUserFromToken: function () {
        //    return $token.get().then(function (token) {
        //        return $http.get("api/user/full", { params: { token: token} }).then(function (res) {
        //            return res.data;
        //        });
        //    });
        //},
        
        //getUserSubscriptionsFromToken: function (skip) {
        //    return $token.get().then(function (token) {
        //        return $http.get("/api/user/subscriptions", { params: { token: token, skip: skip} }).then(function (res) {
        //            return res.data;
        //        });
        //    });
        //},
        passwordRecovery: function (model, root) {
            return $http.post("/api/user/password/recovery", model).then(function(){
                $location.path(root);
            }).catch(function(error){
                return $exception.show(error);
            });
        },
        //getShopSubscriptionsFromToken: function (skip) {
        //    return $token.get().then(function (token) {
        //        return $http.get("/api/shop/subscriber/shops", { params: { token: token, skip: skip} }).then(function (res) {
        //            return res.data;
        //        });
        //    });
        //},
        //saveUser: function (user) {
        //    return $token.get().then(function (token) {
        //        console.log("save user", user);
        //        return $http.post("/api/user/save", user, { params: { token: token }, timeout: $canceler.create().promise });
        //    });
        //},
        //createUser: function (user) {
        //    return $http.post("/api/usercreation/create", user);
        //},
        //getEmptyUser: function () {
        //    return $http.get("/api/usercreation/empty").then(function (res) {
        //        return res.data;
        //    });
        //},
        removeUser: function (userId) {
            return $token.get().then(function (token) {
                return $http.post("/api/user/delete", { userId: userId }, { params: { token: token} });
            });
        },
        getAlertReportFromToken: function () {
            return $token.get().then(function (token) {
                return $http.get("api/reports/alert/shop", { params: { token: token} }).then(function (res) {
                    return res.data;
                });
            });
        },
        getShopReportFromToken: function () {
            return $token.get().then(function (token) {
                return $http.get("api/reports/shop", { params: { token: token} }).then(function (res) {
                    return res.data;
                });
            });
        },
        getUserReportFromToken: function () {
            return $token.get().then(function (token) {
                return $http.get("api/reports/user", { params: { token: token} }).then(function (res) {
                    return res.data;
                });
            });
        },
        //images
        //saveImage64: function (dataUri, sizes) {
        //    if(!Array.isArray(sizes)) sizes = [sizes];
        //    return $http.post("api/image/save64", { datauri: dataUri, sizes: sizes }, { timeout: $canceler.create().promise }).then(function (res) {
        //        return res.data;
        //    });
        //},
        //searchUser: function (val, skip) {
        //    return $http.get("api/search/user", { params: { q: val, skip: skip, limit: 1} }).then(function (res) {
        //        return res.data;
        //    });
        //},
        //searchShop: function (val, skip, limit) {
        //    limit = limit || 1;
        //    return $http.get("api/search/shop", { params: { q: val, skip: skip, limit: limit} }).then(function (res) {
        //        return res.data;
        //    });
        //},
        //searchAlert: function (val, skip) {
        //    return $http.get("api/search/alert", { params: { q: val, skip: skip, limit: 1} }).then(function (res) {
        //        return res.data;
        //    });
        //},
        //searchArchivedAlert: function (val, skip) {
        //    return $http.get("api/archives/alert", { params: { q: val, skip: skip, limit: 3, token: token} }).then(function (res) {
        //        return res.data;
        //    });
        //},
        //tags
        searchTag: function (val, skip, limit) {
            return $http.get("/api/tag/search", { params: { q: val, skip: skip, limit: limit} }).then(function (res) {
                return res.data;
            });
        },
        //shop categories
        
        getShopCategories: function (skip, limit) {
            return $http.get("api/shopcategory", { params: { skip: skip, limit: limit} }).then(function (res) {
                return res.data;
            });
        }
    }
    return new IdentityService();
} ]);